# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import pymysql


class ChengduPipeline(object):
    def __init__(self):
        dbparams = {
            'host': '127.0.0.1',
            'port': 3306,
            'user': 'root',
            'password': 'root',
            'database': 'lianjia',
            'charset': 'utf8'
        }
        self.conn = pymysql.connect(**dbparams)
        self.cursor = self.conn.cursor()
        self._sql = None

    def process_item(self, item, spider):
        print('存')
        self.cursor.execute(self.sql, (item['title'], item['sale_price'], item['publish_price'], item['unit_price'],
                                       item['transaction_days'], item['change_time'], item['look_times'], item['scan_times'],
                                       item['room_size'], item['room_type'], item['floor'], item['publish_time'],
                                       item['created_time'], item['base_info'], item['transaction_info'],item['sale_date'],item['url']))
        self.conn.commit()
        print('存')
        return item

    @property
    def sql(self):
        if not self._sql:
            self._sql = "insert into chengduchengjiao(title,sale_price,publish_price,unit_price,transaction_days,change_time,look_times," \
                        "scan_times,room_size,room_type,floor,publish_time,created_time,base_info,transaction_info,sale_date,url) " \
                        "values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
            return self._sql
        return self._sql
